package com.wx.wxapp.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.wxapp.dao.*;
import com.wx.wxapp.entity.*;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.*;

@Service
public class LabelService {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private  FoodService foodService;

    @Autowired
    private DiseaseService diseaseService;
    /*
     *   功能：获取Food热门标签
     *   参数：
     * */
    @Transactional
    public List<String> GetHotFoodLabel(){
        List<String> Labels = new ArrayList<>();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        FoodlabelMapper foodlabelMapper = sqlSession.getMapper(FoodlabelMapper.class);
        PageHelper.startPage(1,4);
        List<Foodlabel> labels = foodlabelMapper.selectFoodLabelByNumDesc();
        Iterator<Foodlabel> it1 = labels.iterator();
        while (it1.hasNext()){
            String label = it1.next().getLabelname();
            Labels.add(label);
        }
        sqlSession.close();
        return Labels;
    }
    /*
     *   功能：获取Disease热门标签
     *   参数：
     * */
    @Transactional
    public List<String> GetHotDiseaseLabel(){
        List<String> Labels = new ArrayList<>();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DiseaselabelMapper diseaselabelMapper = sqlSession.getMapper(DiseaselabelMapper.class);
        PageHelper.startPage(1,4);
        List<Diseaselabel> labels = diseaselabelMapper.selectDiseaseLabelByNumDesc();
        Iterator<Diseaselabel> it1 = labels.iterator();
        while (it1.hasNext()){
            String label = it1.next().getLabelname();
            Labels.add(label);
        }
        sqlSession.close();
        return Labels;
    }
    /*
     *   功能：根据标签筛选帖子
     *   参数：
     *   label:标签内容
     *   type:标签类别
     * */
    @Transactional
    public Map GetCardsByLabel(String label,int pageNumber,String type){
        System.out.println(type);
        Map map = new HashMap();
        List<List<String>> lists = new ArrayList<>();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PageHelper.startPage(pageNumber,10);
        if (type.equals("food")){
            FoodMapper foodMapper = sqlSession.getMapper(FoodMapper.class);
            FoodimageMapper foodimageMapper = sqlSession.getMapper(FoodimageMapper.class);
            List<Food> foods = foodMapper.selectByLabel(label);
            PageInfo pageInfo = new PageInfo(foods);
            int size = pageInfo.getPages();
            map.put("size",size);
            Iterator<Food> it1 = foods.iterator();
            foodService.FoodCard(lists,foodimageMapper,it1);
            map.put("list",lists);
        }else if (type.equals("disease")){
            DiseaseMapper diseaseMapper =sqlSession.getMapper(DiseaseMapper.class);
            DiseaseimageMapper diseaseimageMapper = sqlSession.getMapper(DiseaseimageMapper.class);
            List<Disease> diseases = diseaseMapper.selectByLabel(label);
            PageInfo pageInfo = new PageInfo(diseases);
            int size = pageInfo.getPages();
            map.put("size",size);
            Iterator<Disease> it1 = diseases.iterator();
            diseaseService.DiseaseCard(lists,diseaseimageMapper,it1);
            map.put("list",lists);
        }
        sqlSession.close();
        return map;
    }
}
